package com.xtianzhuang.www.controller.user.summary;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xtianzhuang.www.annotation.Log;
import com.xtianzhuang.www.annotation.Log.LogType;
import com.xtianzhuang.www.controller.BaseController;
import com.xtianzhuang.www.entity.VUserDailySummary;
import com.xtianzhuang.www.entity.VUserDailySummaryExample;
import com.xtianzhuang.www.entity.constants.CodeConstants;
import com.xtianzhuang.www.entity.request.DeleteSummaryRequest;
import com.xtianzhuang.www.entity.request.SaveSummaryRequest;
import com.xtianzhuang.www.entity.request.SummaryListQueryRequest;
import com.xtianzhuang.www.entity.request.base.BaseRequest;
import com.xtianzhuang.www.entity.response.DeleteSummaryResponse;
import com.xtianzhuang.www.entity.response.SaveSummaryResponse;
import com.xtianzhuang.www.entity.response.SummaryListQueryResponse;
import com.xtianzhuang.www.entity.response.base.BaseResponse;

@Controller
@RequestMapping("/user")
public class SummaryController extends BaseController {
	@RequestMapping(value = "/addSummary")
	public ModelAndView addSummary(BaseRequest request, BaseResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("user/summary-add");
		super.executeBase(request, response);
		modelAndView.addObject("res", response);
		// 跳回原地址
		return modelAndView;
	}

	@RequestMapping(value = "/summaryList")
	public ModelAndView SummaryList(BaseRequest request, BaseResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("user/summary-list");
		super.executeBase(request, response);
		modelAndView.addObject("res", response);
		// 跳回原地址
		return modelAndView;
	}

	@RequestMapping(value = "/ajaxSaveSummary")
	@ResponseBody
	@Log(type = LogType.USER)
	public SaveSummaryResponse ajaxSaveSummary(SaveSummaryRequest request) {
		SaveSummaryResponse response = new SaveSummaryResponse();
		try {
			userServiceBiz.saveSummary(request);
			response.setCode(CodeConstants.SUCCESS);
			response.setMessage("保存成功");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			response.setCode(CodeConstants.FAIL);
			response.setMessage(e.getMessage());
		}
		return response;
	}

	@RequestMapping("/ajaxQuerySummaryList")
	@ResponseBody
	@Log(type = LogType.USER)
	public SummaryListQueryResponse querySummaryList(SummaryListQueryRequest request) {
		SummaryListQueryResponse response = new SummaryListQueryResponse();
		VUserDailySummaryExample example = new VUserDailySummaryExample();
		VUserDailySummaryExample.Criteria criteria = example.createCriteria();
		if (StringUtils.isNotEmpty(request.getDateType())) {
			criteria.andDateTypeEqualTo(request.getDateType());
		}
		if (StringUtils.isNotEmpty(request.getSummaryType())) {
			criteria.andSummaryTypeEqualTo(request.getSummaryType());
		}
		long count = userServiceBiz.userSummaryCountQuery(example);
		example.setOffset(request.getOffset());
		example.setLimit(request.getLimit());
		List<VUserDailySummary> list = userServiceBiz.userSummaryQuery(example);
		response.setCode(CodeConstants.SUCCESS);
		response.setMessage("查询成功");
		response.setData(list);
		response.setTotal(count);
		return response;
	}

	@RequestMapping("/ajaxDeleteSummary")
	@ResponseBody
	@Log(type = LogType.USER)
	public DeleteSummaryResponse deleteSummary(DeleteSummaryRequest request) {
		DeleteSummaryResponse response = new DeleteSummaryResponse();
		userServiceBiz.deleteSummary(request);
		response.setCode(CodeConstants.SUCCESS);
		response.setMessage("删除成功");
		return response;
	}

}
